package com.yzh.wanandroid.component.app

import android.app.Application
import android.content.Context
import androidx.appcompat.app.AppCompatDelegate
import com.yzh.wanandroid.utils.NIGHT_MODE
import com.yzh.wanandroid.utils.SpUtils
import kotlin.properties.Delegates

class MainApplication: Application() {

    companion object {
        // 获取applicationContext
        var appContext: Context by Delegates.notNull()

        // 获取 Application 实例
        lateinit var appInstance: MainApplication
    }

    override fun onCreate() {
        super.onCreate()
        appContext = applicationContext
        appInstance = this
        changeNightMode(SpUtils.getBoolean(NIGHT_MODE,false))
    }

    /**
     * 切换日夜模式
     */
    fun changeNightMode(isNightMode: Boolean) {
        if (isNightMode) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
        } else {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
        }
    }
}